package main

import (
	"go-db/services"
	"go-db/sqldb"
	"go-db/sqldb/dao"
	"go-db/sqldb/entity"
	"log"
	"math/rand"
	"time"

	"github.com/lib/pq"
)

var (
	db      *sqldb.Database
	userDao *dao.User
)

func init() {
	db = sqldb.NewDatabase()
	userDao = &dao.User{}
}

type T struct {
}

func main() {
	rand.Seed(time.Now().UnixNano())
	//Test1()
	Test2()
}

func Test2() {
	db := sqldb.NewDatabase()
	db.LoadPostgres("db/db.properties")
	services.User.Bind(db)
	args := make(map[string]interface{})
	args["Name"] = "harrse7"
	args["Pass"] = "3333"
	userId, err := services.User.Create(args)
	if err != nil {
		log.Fatal(err)
	}
	log.Print("created user: ", userId)
}

func Test1() {
	db.LoadPostgres("db/db.properties")
	userDao.Bind(db)

	if user, err := userDao.GetById(1); err != nil {
		log.Fatal(err)
	} else {
		log.Print(user)
	}

	list, err := userDao.Get(nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Print(list)

	_, err = userDao.Add(
		nil,
		[]string{"Name", "Pass", "TestArray"},
		&entity.User{
			Name:      "hzt'90",
			Pass:      "767'8ddd",
			TestArray: pq.Int64Array{178, 288},
		})
	if err != nil {
		log.Fatal(err)
	}
}
